package com.agileai.bpm.controller.define;

import org.codehaus.jettison.json.JSONObject;

import com.agileai.bpm.common.ProcessDefineUtil;
import com.agileai.bpm.controller.ui.VariableBox;
import com.agileai.bpm.model.ProcessDefineDocument.ProcessDefine;
import com.agileai.bpm.model.TransitionType;
import com.agileai.bpm.model.TransitionType.Type.Enum;
import com.agileai.domain.DataParam;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;

public class WfProcessTransitionEditHandler extends WfProcessInfoHandler{
	public WfProcessTransitionEditHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param){
		String curTransitionId = param.get("transitionId");
		ProcessDefine processDefine = getProcessDefine();
		TransitionType transitionType = ProcessDefineUtil.getTransitionType(processDefine, curTransitionId);
		
		String fromId = transitionType.getFrom();
		String toId = transitionType.getTo();
		String fromName = ProcessDefineUtil.getActivityName(processDefine,fromId);
		String toName = ProcessDefineUtil.getActivityName(processDefine,toId);
		String name = fromName + "--"+ toName;
		setAttribute("transName", name);
		String transType = TransitionType.Type.AUTO.toString();
		setAttribute("WFT_NAME", transitionType.getName());
		setAttribute("WFT_CODE", transitionType.getCode());
		if (transitionType.getType() != null){
			transType = transitionType.getType().toString();
		}
		setAttribute("TRANS_TYPE", transType);
		if (TransitionType.Type.COND.toString().equals(transType)){
			setAttribute("TRANS_EXPRESSION", transitionType.getExpression());	
		}
		setAttribute("fromNodeName", fromName);
		setAttribute("toNodeName", toName);
		setAttribute("WFT_ID", curTransitionId);
		return new LocalRenderer(getPage());
	}
	
	public ViewRenderer doSaveAction(DataParam param){
		String responseText = FAIL;
		String curTransitionId = param.get("WFT_ID");
		String wftName = param.get("WFT_NAME");
		ProcessDefine processDefine = getProcessDefine();
		TransitionType transitionType = ProcessDefineUtil.getTransitionType(processDefine, curTransitionId);
		transitionType.setName(wftName);
		String transType = param.get("TRANS_TYPE");
		transitionType.setType(Enum.forString(transType));
		
		if (TransitionType.Type.COND.toString().equals(transType)){
			String expression = param.get("TRANS_EXPRESSION");
			transitionType.setExpression(expression);
		}
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", curTransitionId);
			jsonObject.put("name", wftName);
			responseText = jsonObject.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	public ViewRenderer doShowVariableBoxAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		variableSelectBox.setDefaultVarType(VariableBox.process_var);
		return variableSelectBox.doShowVariableBoxAction();
	}
	
	public ViewRenderer doChangeVariableTypeAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doChangeVariableTypeAction();
	}
	
	public ViewRenderer doSelectVariableAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
//		String activityId = param.get("WFA_ID");
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doSelectVariableCode();
	}	
	
	public ViewRenderer doBackAction(DataParam param){
		return new RedirectRenderer(getHandlerURL("WfProcessManageList"));
	}
}
